package net.ncraft.mc.easyframework.rabbitmq;

import com.rabbitmq.client.ConnectionFactory;
import net.ncraft.mc.easyframework.FunctionRegister;
import net.ncraft.mc.easyframework.context.ApplicationContext;
import net.ncraft.mc.easyframework.context.anno.Autowired;
import net.ncraft.mc.easyframework.context.anno.Bean;
import net.ncraft.mc.easyframework.context.anno.Component;
import net.ncraft.mc.easyframework.rabbitmq.config.RabbitConfiguration;
import net.ncraft.mc.easyframework.rabbitmq.core.DefaultRabbitTemplate;
import net.ncraft.mc.easyframework.rabbitmq.core.RabbitTemplate;

@Component
public class RabbitAutoRegister implements FunctionRegister {

    @Autowired
    private ApplicationContext context;

    @Autowired
    private RabbitConfiguration rabbitConfiguration;


    @Override
    public void register() {
        if (rabbitConfiguration == null) {
            throw new RuntimeException("请配置消息队列.");
        }

    }


    @Bean(ignoreMethodName = true)
    public RabbitTemplate rabbitTemplate(ConnectionFactory factory) {
        return new DefaultRabbitTemplate(factory);
    }

    @Bean(ignoreMethodName = true, order = 1)
    public ConnectionFactory connectionFactory() {
        if (rabbitConfiguration == null) {
            throw new RuntimeException("请配置消息队列.");
        }
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(rabbitConfiguration.getHost());
        factory.setPort(rabbitConfiguration.getPort());
        factory.setUsername(rabbitConfiguration.getUsername());
        factory.setPassword(rabbitConfiguration.getPassword());
        return factory;
    }


}
